<?php

namespace Learn;

use Learn\DBBehavior;
use Learn\Base\Component;
use Learn\Base\Singleton;

define('APP_ROOT', __DIR__);
function autoload($className)
{
    if (strpos($className, 'Learn\\') === 0) {
        $className = str_replace('Learn\\', '', $className);
    }
    $classFile = APP_ROOT . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $className) . '.php';
    echo $classFile . '<br>';
    include($classFile);
}
spl_autoload_register('Learn\autoload');

class FileDB extends Component
{
    use Singleton;

    private $_filePath;
    private $_fp;
    protected function init($config)
    {
        $this->_filePath = $config['filePath'];
        $this->_fp = fopen($this->_filePath, 'a+');
    }
    public function __destruct()
    {
        fclose($this->_fp);
    }
    public function getFilePointer()
    {
        return $this->_fp;
    }
    // 向csv文件中写入一行数据
    public function insert(array $row)
    {
        fputcsv($this->_fp, $row);
        if ($this->hasEventHandlers('afterInsert')) {
            $this->trigger('afterInsert', $row);
        }
    }
}

$fileDB = FileDB::getInstance(array('filePath' => '../static/resources/test.txt'));
$fileDB->attachBehavior('DBBehavior', new DBBehavior());
$fileDB->insert(['revin', '423923@qq.com']);

echo "The First Line is: " . $fileDB->getFirstRecord();
